<?php


namespace app\services\user;


use app\common\Consts;
use app\models\user\AdminUserModel;
use app\services\BaseService;

class AdminUserService extends BaseService
{

    protected static function initModel()
    {
        return new AdminUserModel();
    }

    public static function createUser($data)
    {
        return self::initModel()::create($data);
    }

    public static function saveUser($id, $data)
    {
        return self::initModel()::update($data, ['id' => $id]);
    }

    /**
     * 获取用户信息
     * @param $userId
     * @return AdminUserModel|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUserInfo($userId)
    {
        $info = self::initModel()::find($userId);
        return $info;
    }

    /**
     * 获取用户信息
     * @param $userName
     * @return AdminUserModel|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUserInfoByAccount($userName)
    {
        $info = self::initModel()::getByUsername($userName);
        return $info;
    }

    /**
     * 获取司机班长下的司机id
     * @param $driverClassId
     * @return array
     */
    public static function getDriverIdArr($driverClassId)
    {
        $idArr = self::initModel()->whereIn('pid', $driverClassId)->column('id');
        return $idArr;
    }

    /**
     * 获取用户昵称
     * @param $userId
     * @return mixed
     */
    public static function getUserNick($userId)
    {
        $nick = self::initModel()->where('id', $userId)->value('nick');
        return $nick;
    }

    /**
     * 获取区域总经理下的司机班长
     * @param $cityId
     * @return array
     */
    public static function getChildDriverClass($cityId)
    {
        $idArr = self::initModel()->where('role_id', Consts::DRIVER_CLASS_ROLE_ID)->where('city_id', $cityId)->column('id');
        return $idArr;
    }

    /**
     * 账号是否存在
     * @param $account
     * @return bool
     */
    public static function getUserByAccount($account)
    {
        $bool = self::initModel()->where('username', $account)->value('id');
        return $bool;
    }
}